package examples;

import java.util.Comparator;

import simpleTaskDispatcher.model.TaskQueue;
import simpleTaskDispatcher.model.WorkersPool;
import simpleTaskDispatcher.model.enums.Priority;
import simpleTaskDispatcher.model.enums.TaskPlacement;
import simpleTaskDispatcher.model.taskDispatchers.TaskQueueComparators;
import simpleTaskDispatcher.model.taskDispatchers.impl.Dispatchers;

public class TwoTaskQueuesExample {

	public static void main(String[] args) throws InterruptedException {
		WorkersPool pool = new WorkersPool();

		pool.createWorkers(4);

		// Default value is 'give equal resources to the two oldest queues' :
		// pool.setDispatcher(Dispatchers.giveEqualResource(2,
		// TaskQueueComparators.compareByAscCreation()));

		// Instead, you can assign workers based on queue's priority. The
		// boolean attribute 'higherPriorityForStartedQueues' let the pool keep
		// focus on a started task queue, even if it has a lower priority. This 
		// reduce context switching.
		Comparator<TaskQueue> comparator = TaskQueueComparators
				.compareByPriority(true);

		// The workers will be assigned to the 3 task queues witch have a higher
		// priority
		pool.setDispatcher(Dispatchers.giveEqualResource(3, comparator));

		//This taskqueue only accept between 1 and 4 workers
		TaskQueue taskQueue1 = new TaskQueue("TaskFeed1", 1, 4);
		//You can also set priority using integers
		taskQueue1.setPriority(Priority.MIDDLE);
		pool.addTaskQueue(taskQueue1);

		TaskQueue taskQueue2 = new TaskQueue("TaskFeed2", 1, 4);
		pool.addTaskQueue(taskQueue2);
		taskQueue1.setPriority(Priority.HIGH);

		//Add some tasks to this task queues
		//Based on this strategy, workers will be dispatched 2/2 (2 workers each)
		
		//Because of this new minimum, workers will be dispatched 3/1 (3 workers for the first taskQueue1)
		taskQueue1.setMinimalWorkers(3, TaskPlacement.PLACE_AT_END_OF_QUEUE);
		taskQueue1.setMaximalWorkers(3, TaskPlacement.PLACE_AT_END_OF_QUEUE);
		
		//Add some tasks to this task queues
		
		
		taskQueue1.close(false);
		taskQueue2.close(false);

		// ...

		pool.closeWorkerPool();
	}

}
